package lk.icta.core.service.impl;

import lk.icta.common.ELandRuntimeException;
import lk.icta.common.ErrorCodes;
import lk.icta.core.dao.URMGOIUserDAO;
import lk.icta.core.entity.URMGOIUser;
import lk.icta.core.service.URMGOIUserManagementService;
import lk.icta.user.common.Permission;
import lk.icta.user.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author Tharanga Punchihewa
 */
public class URMGOIUserManagementServiceImpl implements URMGOIUserManagementService {
    private static final Logger logger = LoggerFactory.getLogger(URMGOIUserManagementServiceImpl.class);
    private final URMGOIUserDAO urmgoiUserDAO;

    public URMGOIUserManagementServiceImpl(URMGOIUserDAO urmgoiUserDAO) {
        this.urmgoiUserDAO = urmgoiUserDAO;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void addORMGOIUser(URMGOIUser urmgoiUser, User user) {
        if (user.isAuthorized(Permission.MANAGE_GOVERNMENT_ORGANIZATION_USER_PROFILE)) {
            logger.debug("Register UR-MGOI user :", urmgoiUser.getUserDetail().getName());
            urmgoiUserDAO.addORMGOIUser(urmgoiUser, user);
        } else {
            logger.debug("No permission");
            handleELandRuntimeException(user.getUsername() + " doesn't have permission to" +
                    " register  UR-MGOI User", ErrorCodes.UNAUTHORIZED_ACCESS);
        }
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void updateORMGOIUser(URMGOIUser urmgoiUser, User user) {
        if (user.isAuthorized(Permission.MANAGE_GOVERNMENT_ORGANIZATION_USER_PROFILE)) {
            logger.debug("Update UR-MGOI user :", urmgoiUser.getUserDetail().getName());
            urmgoiUserDAO.updateORMGOIUser(urmgoiUser, user);
        } else {
            logger.debug("No permission");
            handleELandRuntimeException(user.getUsername() + " doesn't have permission to " +
                    "update  UR-MGOI User", ErrorCodes.UNAUTHORIZED_ACCESS);
        }
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public URMGOIUser getURMGOIByIdUKey(long id, User user) {
        logger.debug("Finding UR-MGOI  User by : {}", user.getUsername());
        return urmgoiUserDAO.getURMGOIByIdUKey(id);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public List<URMGOIUser> getURMGOIUserListByOrganization(int organizationId, User user) {
        return urmgoiUserDAO.getURMGOIUserListByOrganization(organizationId, user);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void activeURMGOIUser(URMGOIUser urmgoiUser, User user) {
        if (user.isAuthorized(Permission.MANAGE_GOVERNMENT_ORGANIZATION_USER_PROFILE)) {
            logger.debug("UR-MGOI User : {}  active by : {}",
                    urmgoiUser.getUserDetail().getName(), user.getUsername());
            urmgoiUser.setStatus(URMGOIUser.State.ACTIVE);
            urmgoiUserDAO.updateORMGOIUser(urmgoiUser, user);
        } else {
            logger.debug("No permission");
            handleELandRuntimeException(user.getUsername() + " doesn't have permission to " +
                    "active  UR-MGOI User", ErrorCodes.UNAUTHORIZED_ACCESS);
        }
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void inactiveURMGOIUser(URMGOIUser urmgoiUser, User user) {
        if (user.isAuthorized(Permission.MANAGE_GOVERNMENT_ORGANIZATION_USER_PROFILE)) {
            logger.debug("UR-MGOI User : {}  inactive by : {}", urmgoiUser.getUserDetail().getName(), user.getUsername());
            urmgoiUser.setStatus(URMGOIUser.State.INACTIVE);
            urmgoiUserDAO.updateORMGOIUser(urmgoiUser, user);
        } else {
            logger.debug("No permission");
            handleELandRuntimeException(user.getUsername() + " doesn't have permission to " +
                    "inactive  UR-MGOI User", ErrorCodes.UNAUTHORIZED_ACCESS);
        }

    }

    @Transactional(propagation = Propagation.REQUIRED)
    public URMGOIUser getURMGOIByUserName(User user) {
        logger.debug("Finding UR-MGOI  User Name : {}", user.getUsername());
        return urmgoiUserDAO.getURMGOIByUserName(user.getUsername());
    }

    private void handleELandRuntimeException(String message, int code) {
        logger.error(message);
        throw new ELandRuntimeException(message, code);
    }
}
